<?php

	$first            = $_POST['first'];            // First name
	$middle           = $_POST['middle'];           // Middle initial (optional)
	$last             = $_POST['last'];             // Last/family name
	$handle           = $_POST['handle'];           // User handle
	$password         = $_POST['password'];         // User password
	$password2        = $_POST['password2'];       // Password confirmation field
	$email            = $_POST['email'];            // Email address
	
	$mysql_host     = "host";  // MySQL Host
	$mysql_database = "db";    // MySQL Database Name
	$mysql_username = "user";  // MySQL Username
	$mysql_password = "pass";  // MySQL Password

	mysql_connect($mysql_host, $mysql_username, $mysql_password) or die ("An error occured in connecting to the MySQL Player Database. Please report this to the webmaster, whose email can be found <a href='contact.html'>here</a>.");
	mysql_select_db($mysql_database);
	
	$user_exists = mysql_fetch_array(mysql_query("SELECT * FROM players WHERE handle='$handle'"));
	$email_exists = mysql_fetch_array(mysql_query("SELECT * FROM players WHERE email='$email'"));
	
	if ($first == "" || $last == "" || $handle == "" || $password == "" || $email == "")
	{
		echo "You must fill in all required fields. <a href='register.html'>Go Back</a>";
	}
	else if ($password !== $password2)
	{
		echo "Passwords dont match. <a href='register.html'>Go Back</a>";
	}
	else if ($user_exists)  // Check if user with specified handle already exists.
	{
		echo "A player with that handle (\"$handle\") already exists. Please enter a different handle. <a href='register.html'>Go Back</a>";
	}
	else if ($email_exists)  // If user with email already exists.
	{
		echo "A player with that email (\"$email\") already exists. <a href='register.html'>Go Back</a>";
	}
	else if (preg_replace('/[a-zA-Z0-9]+', '', $username) !== '')
	{
		echo "Your desired username contains unacceptable characters or too many dashes(-). <a href='register.html'>Go Back</a>";
	}
	else
	{
		mysql_query("INSERT INTO players (handle, password, first_name, last_name, middle_initial, email, rating_standard, rating_blitz, rating_bullet) VALUES (\"$handle\", \"$password\", \"$first\", \"$last\", \"$middle\", \"$email\", 0, 0, 0)") or die (mysql_error());
		echo "Registration successful. You may now log in to the server. ";
	}
?>